<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <title>用户登录</title>
    <style>
        body {
            background-color: #f8f9fa;
            font-family: Arial, sans-serif;
        }
        .container {
            max-width: 400px; /* 控制容器宽度 */
            margin-top: 100px; /* 上边距，使其居中 */
            padding: 20px;
            background-color: #ffffff;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); /* 添加阴影效果 */
        }
        h1, h2 {
            color: #343a40; /* 标题颜色 */
        }
        .error-message {
            color: red;
            display: none;
        }
        button {
            width: 100%; /* 按钮占满宽度 */
        }
        .form-text {
            text-align: center; /* 文本居中对齐 */
        }
        small {
            color: #6c757d; /* 辅助文本颜色 */
        }
    </style>
</head>
<body>
<div class="container">
    <h1 class="text-center">用户登录</h1>

    <div id="login-section" class="mt-4">
        <h2>登录</h2>
        <form id="loginForm">
            <div class="form-group">
                <label for="loginUsername">用户名</label>
                <input type="text" class="form-control" id="loginUsername" placeholder="输入用户名" required>
                <div class="error-message" id="usernameError">用户名不能为空。</div>
            </div>
            <div class="form-group">
                <label for="loginPassword">密码</label>
                <input type="password" class="form-control" id="loginPassword" placeholder="输入密码" required>
                <div class="error-message" id="passwordError">密码不能为空。</div>
            </div>
            <button type="submit" class="btn btn-primary">登录</button>
            <small class="form-text">还没有账号？<a href="#" id="showRegister">注册</a></small>
        </form>
    </div>

    <div id="register-section" class="mt-4" style="display: none;">
        <h2>注册</h2>
        <form id="registerForm">
            <div class="form-group">
                <label for="registerUsername">用户名</label>
                <input type="text" class="form-control" id="registerUsername" placeholder="输入用户名" required>
            </div>
            <div class="form-group">
                <label for="registerPassword">密码</label>
                <input type="password" class="form-control" id="registerPassword" placeholder="输入密码" required>
            </div>
            <div class="form-group">
                <label for="registerName">姓名</label>
                <input type="text" class="form-control" id="registerName" placeholder="输入姓名" required>
            </div>
            <div class="form-group">
                <label for="registerEmail">电子邮件</label>
                <input type="email" class="form-control" id="registerEmail" placeholder="输入邮箱" required>
            </div>
            <button type="submit" class="btn btn-success">注册</button>
            <small class="form-text">已有账号？<a href="#" id="showLogin">登录</a></small>
        </form>
    </div>
</div>

<script>
    $(document).ready(function() {
        // 切换到注册页面
        $('#showRegister').on('click', function() {
            $('#login-section').hide();
            $('#register-section').show();
        });

        // 切换到登录页面
        $('#showLogin').on('click', function() {
            $('#register-section').hide();
            $('#login-section').show();
        });

        // 登录表单提交
        $('#loginForm').on('submit', function(event) {
            event.preventDefault();

            // 清除之前的错误提示
            $('#usernameError').hide();
            $('#passwordError').hide();
            let valid = true;

            const username = $('#loginUsername').val().trim();
            const password = $('#loginPassword').val().trim();

            if (!username) {
                $('#usernameError').show();
                valid = false;
            }

            if (!password) {
                $('#passwordError').show();
                valid = false;
            }

            if (valid) {
                $.ajax({
                    url: 'http://localhost:8088/users/login',
                    type: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify({ username, password }),
                    success: function(response) {
                        if (response.code === 1) {
                            // 登录成功，将用户ID存储到 localStorage
                            localStorage.setItem('userId', response.data.id);
                            localStorage.setItem('username', response.data.username)
                            alert('登录成功！');
                            window.location.href = 'index.html'; // Redirect to main page
                        } else {
                            alert(response.msg);
                        }
                    },
                    error: function() {
                        alert('登录失败，请重试！');
                    }
                });
            }
        });

        // 注册表单提交
        $('#registerForm').on('submit', function(event) {
            event.preventDefault();

            const registerUsername = $('#registerUsername').val().trim();
            const registerPassword = $('#registerPassword').val().trim();
            const registerName = $('#registerName').val().trim();
            const registerEmail = $('#registerEmail').val().trim();

            if (!registerUsername || !registerPassword || !registerName || !registerEmail) {
                alert('请填写所有字段！');
                return;
            }

            // 提交注册信息到后端
            $.ajax({
                url: 'http://localhost:8088/users/register',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify({
                    username: registerUsername,
                    password: registerPassword,
                    name: registerName,
                    email: registerEmail
                }),
                success: function(response) {
                    if (response.code === 1) {
                        alert('注册成功！请登录。');
                        $('#register-section').hide();
                        $('#login-section').show();
                    } else {
                        alert(response.msg);
                    }
                },
                error: function() {
                    alert('注册失败，请重试！');
                }
            });
        });
    });
</script>
</body>
</html>
